package com.zh.harmony.ormlitesample.entity;

import com.j256.ormlite.dao.ForeignCollection;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable;

import java.util.Date;

/**
 * @author wally
 * @date 2022/08/09
 * <p>
 * User用户表数据库实体类
 * <p>
 * 注解：
 * DatabaseTable：通过其中的tableName属性指定数据库名称
 * DatabaseField：代表数据表中的一个字段
 * ForeignCollectionField：一对多关联，表示一个UserEntity关联着多个ArticleEntity（必须使用ForeignCollection集合）
 * <p>
 * 属性：
 * id：当前字段是不是id字段（一个实体类中只能设置一个id字段）
 * columnName：表示当前属性在表中代表哪个字段
 * generatedId：设置属性值在数据表中的数据是否自增
 * useGetSet：是否使用Getter/Setter方法来访问这个字段
 * canBeNull：字段是否可以为空，默认值是true
 * unique：是否唯一
 * defaultValue：设置这个字段的默认值
 */
@DatabaseTable(tableName = "tb_user")
public class UserEntity {
    /**
     * 用户表主键id名称
     */
    public static final String USER_TABLE_COLUMN_NAME_ID = "id";

    /**
     * 主键Id
     */
    @DatabaseField(generatedId = true, columnName = USER_TABLE_COLUMN_NAME_ID, unique = true)
    private Long id;
    /**
     * 昵称
     */
    @DatabaseField(columnName = "nickname")
    private String nickname;
    /**
     * 手机号
     */
    @DatabaseField(columnName = "phone_number")
    private String phoneNumber;
    /**
     * 年龄
     */
    @DatabaseField(columnName = "age")
    private int age;
    /**
     * 性别
     */
    @DatabaseField(columnName = "sex", defaultValue = "1")
    private char sex;
    /**
     * 生日
     */
    @DatabaseField(columnName = "birthday")
    private Date birthday;
    /**
     * 住址
     */
    @DatabaseField(columnName = "address")
    private String address;
    /**
     * 一个用户，关联多个文章
     */
    @ForeignCollectionField(eager = true)
    private ForeignCollection<ArticleEntity> articles;

    public UserEntity() {
    }

    public UserEntity(Long id, String nickname, String phoneNumber, int age, char sex, Date birthday, String address) {
        this.id = id;
        this.nickname = nickname;
        this.phoneNumber = phoneNumber;
        this.age = age;
        this.sex = sex;
        this.birthday = birthday;
        this.address = address;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public ForeignCollection<ArticleEntity> getArticles() {
        return articles;
    }

    public void setArticles(ForeignCollection<ArticleEntity> articles) {
        this.articles = articles;
    }

    @Override
    public String toString() {
        return "UserEntity{" +
                "id=" + id +
                ", nickname='" + nickname + '\'' +
                ", phoneNumber='" + phoneNumber + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                ", birthday=" + birthday +
                ", address='" + address + '\'' +
                '}';
    }
}